package com.swq.java.code.chapter13.code10;

public class Machine extends Thread {
    private static StringBuffer log = new StringBuffer();
    private static int count = 0;

    public void run() {
        for (int a = 0; a < 20; a++) {
            log.append(currentThread().getName() + ":" + a + " ");
            if (++count % 10 == 0) log.append("\n");

//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }

            yield();
        }
    }

    public static void main(String args[]) throws Exception {
        // 验证抢占调度模型
        Machine machine1 = new Machine();
        Machine machine2 = new Machine();
        machine1.setName("m1");
        machine2.setName("m2");
        machine1.start();
        machine2.start();
        while (machine1.isAlive() || machine2.isAlive())
            Thread.sleep(500);  //主线程睡眠500毫秒，等待machine1和machine2线程运行结束
        System.out.println(log);

        // 设置优先级
//        Thread main = Thread.currentThread();
//        main.setPriority(4);
//
//        Machine machine1 = new Machine();
//        Machine machine2 = new Machine();
//        machine1.setName("m1");
//        machine2.setName("m2");
//
//        System.out.println("default priority of main : " + main.getPriority());
//        System.out.println("default priority of m1 : " + machine1.getPriority());
//        System.out.println("default priority of m2 : " + machine2.getPriority());
//
//        machine2.setPriority(MAX_PRIORITY);
//        machine1.setPriority(MIN_PRIORITY);
//
//        machine1.start();
//        machine2.start();
//
//        Thread.sleep(2000);
//        System.out.println(log);

    }
}